package com.liangxiao.wifichat.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.liangxiao.wifichat.R;
import com.liangxiao.wifichat.WifiChatService;
import com.liangxiao.wifichat.model.BaseMessage;

import java.util.ArrayList;
import java.util.List;


/**
 * 备注:
 *
 * @author hmb
 * @version 1.0
 * @since 2015/9/17 16:51
 */
public class ChatRoomFragment extends Fragment implements View.OnClickListener {

    private WifiChatService mService;
    private Handler mHandler;

    private List<BaseMessage> data = new ArrayList<>();

    private EditText content;
    private Button send;
    private ListView listView;
    private Adapter mAdapter;

    public static ChatRoomFragment getInstance(Context context, Handler handler) {
        ChatRoomFragment fragment = new ChatRoomFragment();
        fragment.mHandler = handler;
        fragment.mService = WifiChatService.getInstance(context, handler);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chat_room, container, false);
        content = (EditText) view.findViewById(R.id.content);
        send = (Button) view.findViewById(R.id.send_message);
        send.setOnClickListener(this);
        listView = (ListView) view.findViewById(R.id.listView);

        mAdapter = new Adapter();
        listView.setAdapter(mAdapter);
        return view;
    }

    @Override
    public void onClick(View view) {
        if (mService != null) {
            String msg = content.getText().toString();

            //发送消息
            mService.write(msg.getBytes());

            //清空EditText
            content.setText("");

        } else {
            Toast.makeText(getActivity(), "异常", Toast.LENGTH_SHORT).show();
        }

    }

    public void addData(BaseMessage msg){
        data.add(msg);
		if(mAdapter != null)
        mAdapter.notifyDataSetChanged();
    }

    private class Adapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            view = inflater.inflate(R.layout.chat_room_item, viewGroup, false);

            TextView name = (TextView) view.findViewById(R.id.name);
            TextView msg = (TextView) view.findViewById(R.id.msg);

            BaseMessage message = data.get(i);

            name.setText(message.getName());
            msg.setText(message.getContent());

            return view;
        }
    }
}
